import thread.AuthorNameDisambiguationThread;
import util.PrestoJDBCTool;
import util.SQLConstant;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Xiahu
 * @create 2023/8/3 0003
 * <p>
 * paper、patent作者人名消歧系统数据处理 -- 拼接json数据并请求接口
 * <p>
 * 1、查询未请求接口的人名
 * 2、删除 paper_author_name_tmp、patent_author_name_tmp 数据
 * 3、根据人名,查询paper_author_name 生成 paper_author_name_tmp 数据
 * 4、根据人名,查询patent_author_name 生成 patent_author_name_tmp 数据
 * 5、根据paper_author_name_tmp、patent_author_name_tmp的数据，解析并拼接为请求数据的json
 * 6、请求接口
 * 7、将数据落地到 name_disambiguate 表
 */
public class AuthorNameDisambiguationMainByThread {
    public static Vector<String> vector = new Vector<>();

    static {
        vector.add("1");
        vector.add("2");
        vector.add("3");
        vector.add("4");
        vector.add("5");
        vector.add("6");
        vector.add("7");
        vector.add("8");
        vector.add("9");
        vector.add("10");
    }


    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(1);

        PrestoJDBCTool jdbcTool = new PrestoJDBCTool();

        // 未请求接口的人名
        Map<String, String> notProcessNameMap = new HashMap<>();

        try {
            jdbcTool.connect();
            //1、查询未请求接口的人名
            ResultSet resultSet = jdbcTool.statement.executeQuery(SQLConstant.notProcessAuthorNameQuery);
            while (resultSet.next()) {
                notProcessNameMap.put(resultSet.getString("author_name"), resultSet.getString("query_name"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        for (Map.Entry<String, String> entry : notProcessNameMap.entrySet()) {
            String authorName = entry.getKey();
            if (authorName.contains("'")) {
                continue;
            }

            String flag = null;

            while (true) {
                if (vector.size() > 0) {
                    flag = vector.remove(0);
                    AuthorNameDisambiguationThread thread = new AuthorNameDisambiguationThread(vector, flag, authorName);
                    threadPool.submit(thread);
                    break;
                } else {
                    try {
                        System.out.println("wait 3 s ...");
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }

        threadPool.shutdown();

        try {
            jdbcTool.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
